﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ARR.Entities.Gateways;
using System.Data;
using DDF.Data.Providers;
using DDF.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace ARR.DataModel.Repositories
{
    public static partial class RangeReviewSetupRepository
    {
        public static List<RangeReviewSetupMissingsRow> RangeReviewSetup_CheckMissings(int rangeReviewId, int categoryId, bool checkSales)
        {
            List<RangeReviewSetupMissingsRow> missings = new List<RangeReviewSetupMissingsRow>();
            
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Timeout = 300;

                database.NewCommand("RangeReviewSetup_CheckMissings", CommandType.StoredProcedure);
                database.AddInParameter("@RangeReviewId", DbType.Int32, rangeReviewId);
                database.AddInParameter("@CategoryId", DbType.Int32, categoryId);
                database.AddInParameter("@CheckSales", DbType.Boolean, checkSales);
                
                IDataReader reader = database.ExecuteReader();
                Load(reader, missings);
            }

            return missings;
        }

        private static void Load(IDataReader reader, List<RangeReviewSetupMissingsRow> missings)
        {
            RangeReviewSetupMissingsRow row;

            while (reader.Read())
            {
                row = new RangeReviewSetupMissingsRow()
                {
                    CategoryId = Util.CastDbValue<int>(reader["CategoryId"]),
                    Missings = Util.CastDbValue<int>(reader["Missings"]),
                    Attribute = Util.CastDbValue<string>(reader["Attribute"]),
                    CategoryName = Util.CastDbValue<string>(reader["CategoryName"]),
                    SubCategoryName = Util.CastDbValue<string>(reader["SubCategoryName"]),
                    TypeId = Util.CastDbValue<int>(reader["TypeId"]),
                    ParentId = Util.CastDbValue<int>(reader["ParentId"])
                };

                missings.Add(row);
            }

            reader.Close();
        }
    }
}
